package io.netty.handler.codec.mqtt;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.util.ReferenceCountUtil;

/**
 * Created by amanda.shan on 2019/8/29.
 */
public class MqttUtils {

    private static final ByteBufAllocator ALLOCATOR = new UnpooledByteBufAllocator(false);


    public static byte[] encode(MqttMessage message){

        ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);

        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);

        ReferenceCountUtil.release(byteBuf);

        return bytes;
    }
}
